//Set working directory: cd ""
//install programs if needed
//ssc install tsspell,replace
**Create dataset used for multiverse analysis**
 use "V-Dem-CY-Full+Others-v12.dta",replace
 g lex_dem=0 if e_lexical_index!=.
recode lex (0=1) if e_lex>3
rename e_boix_regime bmr_dem
g vdem_dem=0 if v2x_regime!=.
recode vdem_dem (0=1) if v2x_regime>1
g polity_dem=0 if e_polity2!=.
recode polity (0=1) if e_polity2>5
g fh_dem=0 if e_fh_s!=.
recode fh_dem (0=1) if e_fh_s==1
g lngdp=ln(e_gdppc)
egen poly=std(v2x_polyarchy)
replace e_regiongeo=99 if country_name=="Tuscany" & year==1789//observation does not appear in democracy data. Used to run a multiverse analysis without dropped regions
keep *_dem country_id year lng e_regiongeo v2stfisccap e_pop poly
replace e_pop=ln(e_pop)
 save "vpower.dta",replace
 

**Create dataset (x) for main simulations**
 use "V-Dem-CY-Full+Others-v12.dta",replace
 keep country_name country_id year e_boix_regime 
  egen id=group(country_id)
 rename year t
 rename e_boix x
 drop if x==.
 bys id: egen ever=max(x)
xtset id t
tsspell x
g time_democratic=_seq
replace time_d=0 if x==0
g t2=t*-1
sort id t2
xtset id t2
drop _s* _end
g x2=0 if x!=.
recode x2 (0=1) if x==0 & ever==1
tsspell x2
sort id t
g time_predemocratic=_seq
replace time_pre=0 if x==1
drop x2 t2 _s* _end ever
save "democracy_for_simu.dta",replace

**Create dataset (y) for main simulation**
clear
import delimited "gdppc.csv"
keep if indicator=="latent_gdppc"
keep gwno year mean_log
drop if year<1785
rename mean_log lngdp
rename year t
egen id2=group(gwno)
keep gwno id2 t lngdp
save "gdp_for_simu.dta",replace

**Create dataset (x) for robustness check using polyarchy index**
 use "V-Dem-CY-Full+Others-v12.dta",replace
 keep country_name country_id year v2x_polyarchy 
 rename year t
egen x=std(v2x_polyarchy)
 drop if x==.
   egen id=group(country_id)
 drop v2x
save "democracy_for_simu_poly.dta",replace

**Create dataset for robustness check with civil war onset as Y**
use "hbses_jpr_final.dta",replace 
egen id2=group(cow)
rename year t
rename cow_cw_on onset_cw
keep cow t onset id2
rename cow gwno
save "cw_onset.dta",replace